<?php
	/**
	 * Checks if captcha code ($setupdata['captcha']) on and if so, is it corrent.
	 * 
	 * Returns emptry string on sucsess or if captcha code is off.
	 * Returns return "error" string on failure.
	 * Adds error into glogal $error object if captcha is not correct on failure.
	 * 
	 * Used in form submit, comment submit and forum module
	 * 
	 * @return string
	 */
	function validate_captcha(){
		global $_POST;
		global $setupdata;
		global $_SESSION;
		global $err;
		
		$w_type = '';
		if (isset($setupdata['captcha']) && ($setupdata['captcha'] == "1")){
			if ((!isset($_POST['w_captchacode'])) or
			    (strlen($_POST['w_captchacode']) != 6) or
				(!isset($_SESSION['captcha'])) or
				(strlen($_SESSION['captcha']) != 32))
			{
				$err->AddError(36);
				$w_type = "error";
			}
			else {
				$ch1 = md5("-just-".$_POST['w_captchacode']."-pageit-form-");
				if ($ch1 != $_SESSION['captcha']){
					$err->AddError(36);
					$w_type = "error";
				}
			}
		}
		return $w_type;
	}//validate_captcha()


	if (isset($_GET['corexajax'])){
		$smarty->template_dir = $setupdata['smarty_quest_template_dir'];
		$smarty->register_object("ext2", $smarty_ext2);
		$smarty->assign("_http_path", $setupdata['base_href']);
		$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
		$xajax->processRequests();	
		$mysqlclass->close_db();
		die();
	}
	
	if (isset($setupdata['modules']['siteblocker']['is'])){
		if (($siteblockerclass->get_site_status() == 1) || ($siteblockerclass->is_site_allow() == 1)){
			$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
			$data = $siteblockerclass->get_info();
			$smarty->assign("data", $data);
			$smarty->display('guest/message.html');
			$mysqlclass->close_db();
			include $setupdata['core_dir']."/includes/die.php";
		}
	}
	
	if (isset($setupdata['strip']['guest'])){
		if ($setupdata['strip']['guest'] == 'hard') $smarty->register_prefilter("strip"); 	
		elseif ($setupdata['strip']['guest'] == 'lite') $smarty->load_filter('pre', 'trimwhitespace');
	}

	if (isset($setupdata['smarty']['output']['guest']) && is_array($setupdata['smarty']['output']['guest'])){
		foreach ($setupdata['smarty']['output']['guest'] as $filter) $smarty->load_filter('output', $filter);
	}
	
	$smarty->security = true;
	$smarty->security_settings = array(
		'PHP_HANDLING' => false,
		'PHP_TAGS' => false,
		'INCLUDE_ANY' => false,
		'IF_FUNCS' => array(
			'isset', 'count', 'is_array'
		),
		'MODIFIER_FUNCS' => array(
			'urlencode', 'urldecode', 'htmlspecialchars', 'htmlentities', 'trim', 'stripslashes', 'addslashes', 'base64_encode', 'ucfirst', 'strtoupper', 'html_entity_decode', 'htmlspecialchars_decode'
		)
	);
	$smarty->secure_dir = array(
		$setupdata['smarty_guest_template_dir'], 
		$setupdata['smarty_admin_template_dir']."guest"
	);
	
	include($setupdata['core_dir']."/includes/setlang.php");
	
	if (($_SESSION['lang']==$setupdata['default_languages']) && (!in_array($_SESSION['lang'], $lang_array2))) {
		$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
		$data['text'] = "No current language!";
		$data['email'] = "support@justpageit.lt";
		$smarty->assign("data", $data);
		$smarty->display('guest/message.html');
		$mysqlclass->close_db();
		include $setupdata['core_dir']."/includes/die.php";
	}
	
	require $setupdata['core_dir']."/ext/smarty_ext_guest.php";
	$smarty_ext2=new smarty_ext_guest;
	$smarty_ext->set_ext2($smarty_ext2);
	if (isset($setupdata['languages_switch']) && ($setupdata['languages_switch'] == 1)){
		//$data['active_langs'] = $treeclass->get_site_active_langs($lang_array2);
		$data['active_langs'] = $treeclass->active_langs;
	}
	
	if (!isset($setupdata['local_vars']['last_db_update'])) $setupdata['local_vars']['last_db_update'] = $treeclass->get_last_update_db();
	
	if (isset($setupdata['cache']['block'])){
		if (!isset($setupdata['local_vars']['block_cache_time'])) $setupdata['local_vars']['block_cache_time'] = $treeclass->get_block_cache_time();
	}
	
	$treeclass->get_all_blocks();
	if ((!isset($setupdata['cache']['block']['type'] ) || ($setupdata['local_vars']['last_db_update'] > $setupdata['local_vars']['block_cache_time']))){
		$treeclass->get_all_blocks(1);
	}

 	if (!isset($_GET['no_info'])) $no_info=NULL; else $no_info=$_GET['no_info'];
	if ($no_info==NULL) if (!isset($_POST['no_info'])) $no_info=NULL; else $no_info=$_POST['no_info'];
	if ($no_info !== NULL){
		$smarty->template_dir = $setupdata['smarty_guest_template_dir'];
		$smarty->assign("_var", $setupdata['vars']);
		$smarty->assign("_local_var", $setupdata['local_vars']);
		$smarty->register_object("ext2", $smarty_ext2);
		$smarty->assign("_http_path", $setupdata['base_href']);
		$_last_update = $treeclass->get_last_update_db();
		$smarty->assign("_last_update", $_last_update);
		$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
		$smarty->display($setupdata['default_quest_template']);
		exit;
	}
	
	function get_tree_mas($setupdata, $lang){
		global $treeclass;
		if ($setupdata['tree_cache'] == 0){
			if (isset($setupdata['local_vars']['last_db_update'])) $last_update_db_time = $setupdata['local_vars']['last_db_update'];
			else $last_update_db_time = $treeclass->get_last_update_db();
			if (isset($setupdata['local_vars']['tree_cache_time'])) $tree_cache_time = $setupdata['local_vars']['tree_cache_time'];
			else $tree_cache_time = $treeclass->get_tree_cache_time('guest');
			if (isset($setupdata['cache']['tree']['guest']['type']) && isset($setupdata['cache']['tree']['guest']['path'])){
				$cached_tree = $treeclass->get_cached_tree($setupdata['cache']['tree']['guest']['type'], $setupdata['cache']['tree']['guest']['path'], 'guest');
			} else $cached_tree = $treeclass->get_cached_tree('0', '0', 'guest');
			if (($last_update_db_time > $tree_cache_time) || (!is_array($cached_tree))){
				$lang_array1 = $treeclass->gettreetypes('lang');
				$tree_types = $treeclass->gettreetypes('tree');
				foreach ($lang_array1 as $value) {
					$treeclass->maketree(0, strtolower($value['name']), $tree_types, 1);
					$tree_all[strtolower($value['name'])]=$treeclass->mas;
				}
				if (isset($setupdata['cache']['tree']['guest']['type']) && isset($setupdata['cache']['tree']['guest']['path'])){
					$treeclass->save_cached_tree($tree_all, $setupdata['cache']['tree']['guest']['type'], $setupdata['cache']['tree']['guest']['path'], 'guest');
				} else $treeclass->save_cached_tree($tree_all, '0', '0', 'guest');
			} else $tree_all = $cached_tree;
		} elseif ($setupdata['tree_cache'] == 2) {
			$tree_types = $treeclass->gettreetypes('tree');
			$treeclass->maketree(0, $lang, $tree_types, 1);
			$tree_all[$lang]=$treeclass->mas;
		} else {
			$lang_array1 = $treeclass->gettreetypes('lang');
			$tree_types = $treeclass->gettreetypes('tree');
			foreach ($lang_array1 as $value) {
				$treeclass->maketree(0, strtolower($value['name']), $tree_types, 1);
				$tree_all[strtolower($value['name'])]=$treeclass->mas;
			}
		}
		while (!isset($tree_all[$lang])){
			$setupdata['local_vars']['last_db_update'] = $setupdata['local_vars']['tree_cache_time'] + 10000;
			$tree_all = get_tree_mas($setupdata, $lang);
		}
		$tree=$tree_all[$lang];
		if (isset($setupdata['modules']['clients']['is']) && ($setupdata['modules']['clients']['is'] == 1) && isset($clientsclass)){
			if (isset($_SESSION['login_user']) && (strlen($_SESSION['login_user']) > 0) && (isset($_GET['show_blocks']))){
				$tree=$tree_all[$lang];
			} elseif ((isset($_SESSION['client_login_id'])) && (strlen($_SESSION['client_login_id']) > 0)){
				$tree=$clientsclass->delete_from_tree_childs_no_permission($_SESSION['client_login_id'], $tree_all[$lang]);
			} else {
				$tree=$clientsclass->delete_from_tree_childs_no_permission(0, $tree_all[$lang]);
			}
		} else {
			$tree=$tree_all[$lang];
		}
		$tree_array['tree'] = $tree;
		$tree_array['tree_all'] = $tree_all[$lang];
		$tree_array['tree_all_lang'] = $tree_all;
		return $tree_array;
	}

	if (isset($setupdata['local_vars']['last_db_update'])) $last_update_db_time = $setupdata['local_vars']['last_db_update'];
	else $last_update_db_time = $treeclass->get_last_update_db();

	if (isset($setupdata['local_vars']['tree_cache_time'])) $tree_cache_time = $setupdata['local_vars']['tree_cache_time'];
	else $tree_cache_time = $treeclass->get_tree_cache_time('guest');

	if ((isset($_GET['action'])) and ($_GET['action'] == 'error')) {
		include $setupdata['core_dir']."/includes/errors.php";
	} else {
		if (!isset($_GET['id'])) $id=0; else $id=intval($_GET['id']);
		if ($id==0) unset($_GET['id']);
		if (isset($id) && ($id != 0)) {
			$lang = $treeclass->get_page_lang($id);
			$_SESSION['lang'] = $lang;
		}
		$tree = get_tree_mas($setupdata, $lang);
		$tree_all = $tree['tree_all'];
		$tree_all_lang = $tree['tree_all_lang'];
		$tree = $tree['tree'];
	}

	if (
		(isset($setupdata['not_send_expires_header']) && ($setupdata['not_send_expires_header'] == 1)) ||
		(isset($_SESSION['client_login_id']) && (strlen($_SESSION['client_login_id']) > 0)) ||
		(isset($setupdata['smarty_guest_cache']) && ($setupdata['smarty_guest_cache'] == 1)) ||
		($last_update_db_time > $tree_cache_time)
		){
		header("Date: ".gmdate("D, d M Y H:i:s", $last_update_db_time)." GMT");
		header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_update_db_time)." GMT");
		header("X-Powered-By: JustPageIt 1.0");
		header("Expires: ".gmdate("D, d M Y H:i:s T", $last_update_db_time-86400)); // cache off
	} else {
		if (($setupdata['tree_cache'] == 0) && ($last_update_db_time > $tree_cache_time)) header ("HTTP/1.1 205 Reset Content");
		header("Pragma: cache"); // cache on
		header("Cache-Control: private"); // cache on
		header("Date: ".gmdate("D, d M Y H:i:s", $last_update_db_time)." GMT");
		header("ETag: ".gmdate("D, d M Y H:i:s", $last_update_db_time)." GMT"); // cache on
		header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_update_db_time)." GMT");
		header("X-Powered-By: JustPageIt 2.0");
		header("Expires: ".gmdate("D, d M Y H:i:s T", $last_update_db_time+86400)); // cache on
	}
	
	if (!isset($_GET['id'])) $id=0; else $id=intval($_GET['id']);
	if ($id == 0) unset($_GET['id']);
	
	if (!isset($treeclass->active_langs[$lang])) $id = 0;
	
 	if (!isset($_GET['name'])) $name=NULL; else $name=$_GET['name'];
 	if (!isset($_GET['type'])) $type=NULL; else $type=$_GET['type'];
	if ($type==NULL) if (!isset($_POST['type'])) $type=NULL; else $type=$_POST['type'];
	if (!isset($_GET['show_menu'])) $show_menu=NULL; else $show_menu=$_GET['show_menu'];
	
 	if (!isset($_GET['act'])) $act=NULL; else $act=$_GET['act'];
	if ($act==NULL) if (!isset($_POST['act'])) $act=NULL; else $act=$_POST['act'];
	if (!isset($_GET['mod'])) $mod=NULL; else $mod=$_GET['mod'];
	if ($mod==NULL) if (!isset($_POST['mod'])) $mod=NULL; else $mod=$_POST['mod'];
 	if (!isset($_GET['form_id'])) $form_id=NULL; else $form_id=$_GET['form_id'];
	if ($form_id==NULL) if (!isset($_POST['form_id'])) $form_id=NULL; else $form_id=$_POST['form_id'];
 	if (!isset($_GET['form_save'])) $form_save=NULL; else $form_save=$_GET['form_save'];
	if ($form_save==NULL) if (!isset($_POST['form_save'])) $form_save=NULL; else $form_save=$_POST['form_save'];
 	if (!isset($_GET['parent'])) $parent=NULL; else $parent=$_GET['parent'];
	if ($parent==NULL) if (!isset($_POST['parent'])) $parent=NULL; else $parent=$_POST['parent'];

	if (!isset($_GET['query'])) $query=NULL; else $query=$_GET['query'];
	if ($query==NULL) if (!isset($_POST['query'])) $query=NULL; else $query=$_POST['query'];

	if (!isset($_GET['search_type'])) $search_type=NULL; else $search_type=$_GET['search_type'];
	if ($search_type==NULL) if (!isset($_POST['search_type'])) $search_type=NULL; else $search_type=$_POST['search_type'];

	if (!isset($_GET['rid'])) $rid=NULL; else $rid=$_GET['rid'];
	if ($rid==NULL) if (!isset($_POST['rid'])) $rid=NULL; else $rid=$_POST['rid'];
	if (($rid!=NULL) && (isset($setupdata['modules']['realstate']['is'])) && (file_exists($setupdata['core_dir']."/modules/realstate/realstate_guest.php"))) {
		include $setupdata['core_dir']."/modules/realstate/realstate_guest.php";
	}	

	if (!isset($_GET['pid'])) $pid=NULL; else $pid=$_GET['pid'];
	if ($pid==NULL) if (!isset($_POST['pid'])) $pid=NULL; else $pid=$_POST['pid'];
	if (($pid!=NULL) && (isset($setupdata['modules']['shop']['is'])) && (file_exists($setupdata['core_dir']."/modules/shop/shop_guest.php"))) {
		include $setupdata['core_dir']."/modules/shop/shop_guest.php";
	}

	if (!isset($_GET['cid'])) $cid=NULL; else $cid=$_GET['cid'];
	if ($cid==NULL) if (!isset($_POST['cid'])) $cid=NULL; else $cid=$_POST['cid'];
	if (($cid!=NULL) && (isset($setupdata['modules']['contacts']['is'])) && (file_exists($setupdata['core_dir']."/modules/contacts/contacts_guest.php"))) {
		include $setupdata['core_dir']."/modules/contacts/contacts_guest.php";
	}	

	if (!isset($_GET['did'])) $did=NULL; else $did=$_GET['did'];
	if ($did==NULL) if (!isset($_POST['did'])) $did=NULL; else $did=$_POST['did'];
	if (($did!=NULL) && (isset($setupdata['modules']['docum']['is'])) && (file_exists($setupdata['core_dir']."/modules/docum/docum_guest.php"))) {
		include $setupdata['core_dir']."/modules/docum/docum_guest.php";
	}	
		
	if (!isset($_GET['module'])) $module=NULL; else $module=$_GET['module']; 
	if ($module==NULL) if (!isset($_POST['module'])) $module=NULL; else $module=$_POST['module']; 
	
	if (($module != NULL) && (isset($setupdata['modules'][$module]['is'])) && (isset($setupdata['modules']['plugins']['path']) && file_exists($plugin_dir."/modules/".$module."/".$module."_guest.php"))){
		include $plugin_dir."/modules/".$module."/".$module."_guest.php";
	} elseif (($module != NULL) && (isset($setupdata['modules'][$module]['is'])) && (file_exists($setupdata['core_dir']."/modules/".$module."/".$module."_guest.php"))) {
		include $setupdata['core_dir']."/modules/".$module."/".$module."_guest.php";
	} else {
		if ($act == "form_submit"){
			$w_type = validate_captcha();
			
			if ($w_type == "") {
				foreach($_POST as $key=>$el){
					if ((strpos($key, "form_data_") !== false) and (strpos($key, "form_data_") == 0))
					{
						$mail_data[$key] = $el;
						if( $el['type'] == "email" )
						{
							$mail_settings['sender_email'] = $el['value'];
						}
					}
					elseif ((strpos($key, "form_") !== false) and (strpos($key, "form_") == 0)) $mail_settings[$key] = $el;
				}
				$form_field_id = $smarty_ext->get_field_id(array('parent'=>$form_id, 'name'=>'block_form_data'));
				$form_fields = $treeclass->get_form_elem_data($form_field_id);
				$form_main = $treeclass->get_item_content($form_id);
			
			/*
				print_r($data);
				print_r($_FILES);
				print_r($_GET);
				print_r($form_fields);
			*/			
				
				$form_valided = true;
				if (isset($_FILES) && (count($_FILES) > 0)){
					foreach($form_fields as $key=>$el){
						if (($form_fields[$key][$key.'_req01']['value'] == "on") && ($form_fields[$key][$key.'_type01']['value'] == "file")){
							if (isset($_FILES["form_data_".$key]['error']) && ($_FILES["form_data_".$key]['error'] == 0)){
							} else {
								$form_valided = FALSE;
							}
						}
					}
				}
				
				if ($form_valided === FALSE){
					 $err->AddError(23);
				} else {
					if ((!isset($form_main['block_form_to']['value'])) || (isset($form_main['block_form_to']['value']) && (strlen($form_main['block_form_to']['value']) == 0)) || ((strlen($form_main['block_form_to']['value']) > 0) && isset($mail_settings['form_to']) && ($form_main['block_form_to']['value'] == $mail_settings['form_to']))){
						if (is_array($form_fields) && (count($form_fields)>0)){
							foreach($mail_data as $key=>$el){
								$k = substr($key, 10, 2);
								if (array_key_exists($k, $form_fields)){
									if (isset($form_fields[$k][$k.'_mail01']['value']) && ($form_fields[$k][$k.'_mail01']['value'] == 'on')){	
										if (!isset($mail_settings['form_to'])) $mail_settings['form_to'] = $el['value'];
										else $mail_settings['form_to'] .= ", ".$el['value'];
									}
								}
							}
						}
						if (!isset($_POST['form_no_db'])) $treeclass->save_form_to_db($parent, $form_id, $mail_data, $_FILES);
						$smarty->assign("mail_data", $mail_data);
						$smarty->template_dir = $setupdata['smarty_guest_template_dir'];
						if (isset($_POST['form_template'])){
							$tempalate = "guest/mail/".$_POST['form_template'];
							if ($smarty->template_exists($tempalate)) $output = $smarty->fetch($tempalate);
							else $output = "No html mail template found!";
						} else $output = "No html mail template found!";
						if (isset($_POST['form_template_text'])){
							$tempalate = "guest/mail/".$_POST['form_template_text'];
							if ($smarty->template_exists($tempalate)) $output_text = $smarty->fetch($tempalate);
							else $output_text = "No text mail template found!";
						} else $output_text = "No text mail template found!";
						$smarty->clear_assign("mail_data");
						if ($mod == 'form'){
							if (isset($mail_settings['form_from_name'])) $mail_settings['form_from'] = '"'.trim($mail_settings['form_from_name']).'" <'.$mail_settings['form_from'].'>';
							if (isset($mail_settings['form_to_name'])){
								$mail_array = preg_split("/[\s,;]+/", $mail_settings['form_to'], -1, PREG_SPLIT_NO_EMPTY);
								foreach($mail_array as $key=>$mail){
									if ($key == 0) $mail_settings['form_to'] = '"'.trim($mail_settings['form_to_name']).'" <'.$mail_array[$key].'>';
									else $mail_settings['form_to'] .= ", ".$mail_array[$key];
									//if ($key == 0) $mail_settings['form_to'] = $mail_array[$key];
									//else $mail_settings['form_to'] .= ", ".$mail_array[$key];
								}
							}
						}
						if (isset($setupdata['vars']['type']) && ($setupdata['vars']['type'] == 1)){
							$treeclass->send_form_to_mail($output, $output_text, $mail_settings, $_FILES);
						} else {
							$treeclass->send_form_to_mail($output, $output_text, $mail_settings, $_FILES);
						}
					} else {
						// nera meilo, arba mailas nesutampa
					}
				}
			}
			
		} elseif ($act == "vote_submit"){
			foreach($_POST as $key=>$el){
				if ((strpos($key, "vote_data") !== false) and (strpos($key, "vote_data") == 0)){
					if (isset($_POST['vote_data'])) $res = $treeclass->save_vote_to_db($parent, $_POST['vote_data']);
					else $res = 10;
					if ($res != 0) $err->AddError($res, "vote");
					else {
						$treeclass->get_all_blocks();
						if ((!isset($setupdata['cache']['block']['type'] ) || ($setupdata['local_vars']['last_db_update'] > $setupdata['local_vars']['block_cache_time']))){
							$treeclass->get_all_blocks(1);
						}
					}
				}
			}
		} elseif ($act == "coments_submit"){
			$w_type = validate_captcha();
			
			if ($w_type == "") {
				$comm_mas = array();
				$comm_type_array = $treeclass->get_comments_fields($id);
				foreach($_POST as $key=>$el){
					foreach($comm_type_array as $k=>$field){
						if ($field['name'] == $key) {
							$comm_mas[$key]['name'] = $comm_type_array[$k]['name'];
							$comm_mas[$key]['type'] = $comm_type_array[$k]['type_of_value'];
							$comm_mas[$key]['value'] = $el;
						}
					}
				}
				$treeclass->save_coments_to_db($parent, $comm_mas);
				unset($comm_mas);
			}
			if (isset($_GET['referer']) || isset($_POST['referer'])){
				require ($setupdata['core_dir'].'/includes/referer.php');
			} else {
				header ("location: ".$_SERVER['HTTP_REFERER']);
				exit();
			}
		}
		if (($query!=NULL) && (!is_array($query)) && (strlen($query) > 0)){
			$query = strip_tags($query);
			$data['search'] = $treeclass->tree_search($query, $search_type);
		} else $data['search'] = "";
	}
	$data['path']=$treeclass->sqlpath($id);
	if ($id > 0) {
		if (isset($setupdata['modules']['clients']['is']) && ($setupdata['modules']['clients']['is'] == 1) && isset($clientsclass)){
			if ((isset($_SESSION['client_login_id'])) && (strlen($_SESSION['client_login_id']) > 0)) $client_id = $_SESSION['client_login_id'];
			else $client_id = NULL;
			if (($clientsclass->is_client_access_to_page_from_guest($id, $client_id)==1) or	(isset($_SESSION['login_user']) && (strlen($_SESSION['login_user']) > 0) && (isset($_GET['show_blocks'])))){
				if (count($data['path']) > 0) {
					$data['page']=$data['path'][count($data['path'])-1];
					if (isset($setupdata['modules']['clients']['is']) && ($setupdata['modules']['clients']['is'] == 1) && isset($clientsclass)){
						if (isset($_SESSION['login_user']) && (strlen($_SESSION['login_user']) > 0) && (isset($_GET['show_blocks']))){
							$tree=$tree_all;
						} elseif ((isset($_SESSION['client_login_id'])) && (strlen($_SESSION['client_login_id']) > 0)){
							$tree=$clientsclass->delete_from_tree_childs_no_permission($_SESSION['client_login_id'], $tree_all);
						} else {
							$tree=$clientsclass->delete_from_tree_childs_no_permission(0, $tree_all);
						}
					}
					foreach($tree as $tree_stick) if ($tree_stick['id'] == $data['page']['parent']) $data['page']['parent'] = $tree_stick;
				} else {
					$data['page'] = 0;
					if (isset($setupdata['modules']['clients']['is']) && ($setupdata['modules']['clients']['is'] == 1) && isset($clientsclass)){
						if (isset($_SESSION['login_user']) && (strlen($_SESSION['login_user']) > 0) && (isset($_GET['show_blocks']))){
							$tree=$tree_all;
						} elseif ((isset($_SESSION['client_login_id'])) && (strlen($_SESSION['client_login_id']) > 0)){
							$tree=$clientsclass->delete_from_tree_childs_no_permission($_SESSION['client_login_id'], $tree_all);
						} else {
							$tree=$clientsclass->delete_from_tree_childs_no_permission(0, $tree_all);
						}
					}
				}
				$data['content'] = $treeclass->get_item_content($id);
				foreach ($data['content'] as $key=>$val) $data['content1'][$key] = $val['value'];
				$data['elements'] = $treeclass->_get_page_elements_with_content ($id, 1);
			} else {
				$data['page']['id'] = 0;
				$data['content'] = array();
				$data['content1'] = array();
				$data['elements'] = array();
				$data['path'] = array();
				if (isset($setupdata['modules']['clients']['is']) && ($setupdata['modules']['clients']['is'] == 1) && isset($clientsclass)){
					if (isset($_SESSION['login_user']) && (strlen($_SESSION['login_user']) > 0) && (isset($_GET['show_blocks']))){
						$tree=$tree_all;
					} elseif ((isset($_SESSION['client_login_id'])) && (strlen($_SESSION['client_login_id']) > 0)){
						$tree=$clientsclass->delete_from_tree_childs_no_permission($_SESSION['client_login_id'], $tree_all);
					} else {
						$tree=$clientsclass->delete_from_tree_childs_no_permission(0, $tree_all);
					}
				}
			}
		} else {
			if (count($data['path']) > 0) {
				$data['page']=$data['path'][count($data['path'])-1];
				$tree=$tree_all;
				foreach($tree as $tree_stick) if ($tree_stick['id'] == $data['page']['parent']) $data['page']['parent'] = $tree_stick;
			} else {
				$data['page'] = 0;
				$tree=$tree_all;
			}
			$data['content'] = $treeclass->get_item_content($id);
			foreach ($data['content'] as $key=>$val) $data['content1'][$key] = $val['value'];
			$data['elements'] = $treeclass->_get_page_elements_with_content ($id, 1);
		}
	} else { 
		$data['page']['id'] = 0;
		if (isset($setupdata['modules']['clients']['is']) && ($setupdata['modules']['clients']['is'] == 1) && isset($clientsclass)){
			if (isset($_SESSION['login_user']) && (strlen($_SESSION['login_user']) > 0) && (isset($_GET['show_blocks']))){
				$tree=$tree_all;
			} elseif ((isset($_SESSION['client_login_id'])) && (strlen($_SESSION['client_login_id']) > 0)){
				$tree=$clientsclass->delete_from_tree_childs_no_permission($_SESSION['client_login_id'], $tree_all);
			} else {
				$tree=$clientsclass->delete_from_tree_childs_no_permission(0, $tree_all);
			}
		}
	}

	
	if (isset($setupdata['modules']['realstate']['is'])) {
		if ((isset($data['page']['type'])) && ($data['page']['type'] == 'realstate')){
			if ($data['content1']['realstate_products_group_id'] != ""){
				if (isset($setupdata['modules']['realstate']['is'])) {
					$data['rproducts'] = $realstateclass->get_all_products_by_group($data['content1']['realstate_products_group_id']);
				}	
			}
		}
	}
	
	if (isset($setupdata['modules']['shop']['is'])) {
		if ((isset($data['page']['type'])) && ($data['page']['type'] == 'shop')){
			if ($data['content1']['shop_products_group_id'] != ""){
				if (isset($setupdata['modules']['shop']['is'])) {
					$data['products'] = $shopclass->get_all_products_by_group($data['content1']['shop_products_group_id']);
				}	
			}
		}
	}

	if (isset($setupdata['modules']['clients']['is'])) {
		$client_info = $clientsclass->client_info;
		if (!isset($client_info['main']['id']) || ($client_info['main']['id'] != $_SESSION['client_login_id'])){
			$clientsclass->get_curent_client_info();
			$client_info = $clientsclass->client_info;
		}
		$data['client'] = $client_info;
	}
	
			
	$data['site'] = $treeclass->_get_site_settings($id);
	
	$temp_err = array();
	if (isset($_SESSION['errors'])){
		$temp_errs = unserialize($_SESSION['errors']);
		if (isset($temp_errs['form_id'])){
			$temp_err = $temp_errs['form_id'];
			unset($temp_errs['form_id']);
			$_SESSION['errors'] = serialize($temp_errs);
		} else {
			$temp_err = $temp_errs;
		}
	}
	
	$data['errors'] = $temp_err;
	unset($temp_errs); 	unset($temp_err);

	if (isset($data['page']['lang'])) {if (in_array($data['page']['lang'], $lang_array2)) $_SESSION['lang'] = $data['page']['lang'];}

	if (isset($_GET['getxml'])){
		$xmlclass->make($data);
		echo $xmlclass->get();
		$mysqlclass->close_db();
		include $setupdata['core_dir']."/includes/die.php";
	}

	if ($is_xajax !== false){
		$xajax->processRequests();
		$smarty->assign('xajax_javascript', $xajax->getJavascript("_tvs_system_core_/js/","xajax.js"));
	}
	
	if (isset($_GET['referer']) || isset($_POST['referer'])){
		require ($setupdata['core_dir'].'/includes/referer.php');
	} else {
		$smarty->template_dir = $setupdata['smarty_guest_template_dir'];
		$smarty->assign("_var", $setupdata['vars']);
		$smarty->assign("_local_var", $setupdata['local_vars']);
		$smarty->register_object("ext2", $smarty_ext2);
		$smarty->assign("tree", $tree);
		$smarty->assign("data", $data);
		$smarty->assign("_http_path", $setupdata['base_href']);
		$_last_update = $treeclass->get_last_update_db();
		if (isset($setupdata['local_vars']['last_db_update'])) $smarty->assign("_last_update", $setupdata['local_vars']['last_db_update']);
		else $smarty->assign("_last_update", $_last_update);
	
		if (isset($setupdata['smarty_guest_cache']) && $setupdata['smarty_guest_cache'] == 0 && (!isset($_SESSION['client_login_id']) || (strlen($_SESSION['client_login_id']) == 0))){
			$smarty->caching = true;
			$cook = $_COOKIE;
			foreach($_COOKIE as $key=>$COOKIE) {if ((strpos($key, "vote_cookie_") !== false)) unset($cook[$key]);}
			if (isset($cook['tvs_session_id'])) unset($cook['tvs_session_id']);
			if (isset($cook['interface_lang'])) unset($cook['interface_lang']);
			if (isset($cook['shop_id'])) unset($cook['shop_id']);
			if (isset($cook['login_username'])) unset($cook['login_username']);
			if (isset($_SESSION['errors'])) $error = $_SESSION['errors'];
			else $error = "";
	
			$cache_id = md5(serialize(array($_SERVER['REQUEST_URI'], $_GET, $_POST, $cook, $error)));
		
			if ((!isset($setupdata['local_vars']['smarty_caching_time'])) || ($setupdata['local_vars']['last_db_update'] > $setupdata['local_vars']['smarty_caching_time'])){
				if ($_SERVER['REMOTE_ADDR'] == '195.14.168.190') $smarty->clear_all_cache(1);
				else $smarty->clear_all_cache();
				$sql = $mysqlclass->mysql_my_query("SELECT * FROM `".$setupdata['table_prefix']."local_vars` WHERE `name`='smarty_caching_time'", __LINE__, __FILE__);
				if ($mysqlclass->mysql_my_num_rows($sql) == 0) $sql = $mysqlclass->mysql_my_query("INSERT INTO `".$setupdata['table_prefix']."local_vars` ( `id` , `name` , `value` ) VALUES ('', 'smarty_caching_time', '".time()."')", __LINE__, __FILE__);
				else $sql = $mysqlclass->mysql_my_query("UPDATE `".$setupdata['table_prefix']."local_vars` SET `value`='".time()."' WHERE `name` = 'smarty_caching_time'", __LINE__, __FILE__);
			}
			$smarty->caching = true;
			if ((isset($_POST['module']) && $_POST['module'] == 'shop_cart')) $smarty->cache_modified_check = true;
			$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
			$smarty->display($setupdata['default_quest_template'], $cache_id);
		} else {
			$smarty->caching = false;
			if (!isset($setupdata['smarty_guest_cache']) || ($setupdata['smarty_guest_cache'] == 1)){
				if ($_SERVER['REMOTE_ADDR'] == '195.14.168.190') $smarty->clear_all_cache(2);
				else $smarty->clear_all_cache();
			}
			$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
			$smarty->display($setupdata['default_quest_template']);
		}
	}
	
	unset($tree_all);
	unset($tree);
	unset($data);
	$err->DeleteErrorSession();	
?>
